#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "audioclient.h"
#include "playlistmodel.h"
#include <QFileDialog>
#include <QItemSelectionModel>
#include <QSortFilterProxyModel>

MainWindow::MainWindow(QWidget *parent, AudioClient* xmmsclient)
    : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
    client = xmmsclient;
    playlistQueueModel = new PlaylistModel(client);
    playlistProxyModel = new QSortFilterProxyModel(this);
    playlistProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
    playlistProxyModel->setFilterKeyColumn(-1);
    sliderIsPressed = false;
    ui->setupUi(this);
    playlistProxyModel->setSourceModel(playlistQueueModel);
    ui->treeView->setModel(playlistProxyModel);

    hideJumpBar();

    connect(ui->actionPlay, SIGNAL(triggered()), client, SLOT(play()));
    connect(ui->actionPause, SIGNAL(triggered()), client, SLOT(pause()));
    connect(ui->actionStop, SIGNAL(triggered()), client, SLOT(stop()));
    connect(ui->actionPrevious, SIGNAL(triggered()), client, SLOT(prev()));
    connect(ui->actionNext, SIGNAL(triggered()), client, SLOT(next()));
    connect(ui->actionAdd_media, SIGNAL(triggered()), this, SLOT(addFilesToPlaylist()));
    connect(ui->actionRemove_selected, SIGNAL(triggered()), this, SLOT(removeSelectedEntries()));
    connect(ui->actionJump, SIGNAL(triggered()), this, SLOT(showJumpBar()));

    connect(ui->playButton, SIGNAL(clicked()), client, SLOT(play()));
    connect(ui->pauseButton, SIGNAL(clicked()), client, SLOT(pause()));
    connect(ui->stopButton, SIGNAL(clicked()), client, SLOT(stop()));
    connect(ui->prevButton, SIGNAL(clicked()), client, SLOT(prev()));
    connect(ui->nextButton, SIGNAL(clicked()), client, SLOT(next()));
    connect(ui->jumpCancelButton, SIGNAL(clicked()), this, SLOT(hideJumpBar()));
    connect(ui->clearButton, SIGNAL(clicked()), client, SLOT(clear()));

    connect(ui->treeView, SIGNAL(activated(QModelIndex)), this, SLOT(playSelected(QModelIndex)));
    connect(ui->jumpLineEdit, SIGNAL(textChanged(QString)), playlistProxyModel, SLOT(setFilterFixedString(QString)));
    connect(client, SIGNAL(playtime(int)), this, SLOT(updatePlaytime(int)));
    connect(client, SIGNAL(currentSongDuration(int)), this, SLOT(updateCurrentDuration(int)));
    connect(client, SIGNAL(currentPosition(unsigned int)), this, SLOT(updateCurrentPosition(unsigned int)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

int MainWindow::updateCurrentDuration(const int& value){
    ui->positionSlider->setMaximum(value);
    return true;
}

void MainWindow::stopSliderUpdate(){
    sliderIsPressed = true;
}

//Connect this slot to setup the ui according to the playlist's entry in newPosition
void MainWindow::updateCurrentPosition(const unsigned int& newPosition){
    QString songTitle = playlistQueueModel->item(newPosition)->text();
    this->setWindowTitle(songTitle);
    playlistQueueModel->setCurrentPosition(newPosition);
}

void MainWindow::updateSongPosition(){
    int position = ui->positionSlider->value();
    sliderIsPressed = false;
    client->seek(position);
}

void MainWindow::updatePlaytime(const int& value){
    if (!sliderIsPressed)
        ui->positionSlider->setValue(value);
}

void MainWindow::addFilesToPlaylist(){
    QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Select media"));
    for (QStringList::iterator fileName = fileNames.begin(), fileName_end = fileNames.end();
        fileName != fileName_end;
        ++fileName){
            client->add(*fileName);
        }
}

void MainWindow::removeSelectedEntries(){
    QModelIndexList indexList = ui->treeView->selectionModel()->selectedRows();
    qSort(indexList.begin(), indexList.end());
    int row;
    while (!indexList.isEmpty()){
        row = playlistProxyModel->mapToSource(indexList.takeLast()).row();
        client->playlistRemoveEntry(row);
    }
}

void MainWindow::showJumpBar(){
    ui->jumpLabel->show();
    ui->jumpLineEdit->show();
    ui->jumpCancelButton->show();
    ui->jumpLineEdit->setFocus();
    playlistProxyModel->setFilterFixedString(ui->jumpLineEdit->text());
    //ui->treeView->selectionModel()->select(playlistProxyModel->index(0, 0), QItemSelectionModel::SelectCurrent|QItemSelectionModel::Rows);
}

void MainWindow::hideJumpBar(){
    ui->jumpLabel->hide();
    ui->jumpLineEdit->hide();
    ui->jumpCancelButton->hide();
    playlistProxyModel->setFilterFixedString("");
}

void MainWindow::playSelected(const QModelIndex& selectedIndex){
    client->play(playlistProxyModel->mapToSource(selectedIndex));
}
